package config;

import java.io.File;
import java.io.FilenameFilter;

public class FileManager {

	private CLInterface cl;
	private String tfdir;
	
	public FileManager(String dir, CLInterface cl) {
		this.cl = cl;
		tfdir = dir;
	}
	
	public void replaceCfg() {
		while(!checkCfg()) {
			if(!cl.rename(tfdir + "\\cfg", "lwrtcfg")) cl.mkdir(tfdir + "\\lwrtcfg");
			cl.copy("cfg", tfdir + "\\cfg");
		}
	}
	
	public void restoreCfg() {
		while(checkCfg()) {
			cl.rmdir(tfdir + "\\cfg");
			cl.rename(tfdir + "\\lwrtcfg", "cfg");
		}
	}
	
	public void replaceHud(String hud) {
		while(!checkResource()) {
			if(!cl.rename(tfdir + "\\resource", "lwrtresource")) cl.mkdir(tfdir + "\\lwrtresource");
			cl.copy("hud\\" + hud + "\\resource", tfdir + "\\resource");
		}
		
		while(!checkScripts()) {
			if(!cl.rename(tfdir + "\\scripts", "lwrtscripts")) cl.mkdir(tfdir + "\\lwrtscripts");
			cl.copy("hud\\" + hud + "\\scripts", tfdir + "\\scripts");
		}
	}
	
	public void restoreHud() {
		while(checkResource()) {
			cl.rmdir(tfdir + "\\resource");
			cl.rename(tfdir + "\\lwrtresource", "resource");
		}
		
		while(checkScripts()) {
			cl.rmdir(tfdir + "\\scripts");
			cl.rename(tfdir + "\\lwrtscripts", "scripts");
		}
	}
	
	
	public void replaceVo() {
		while(!checkVo()) {
			if(!cl.rename(tfdir + "\\sound\\vo", "lwrtvo")) cl.mkdir(tfdir + "\\sound\\lwrtvo");
			cl.copy("sound\\vo", tfdir + "\\sound\\vo");
		}
	}
	
	public void restoreVo() {
		while(checkVo()) {
			cl.rmdir(tfdir + "\\sound\\vo");
			cl.rename(tfdir + "\\sound\\lwrtvo", "vo");
		}
	}
	
	public void replaceMisc(boolean dom, boolean ann) {
		while(!checkMisc()) {
			if(!cl.rename(tfdir + "\\sound\\misc", "lwrtmisc")) cl.mkdir(tfdir + "\\sound\\lwrtmisc");
			cl.copy("sound\\misc", tfdir + "\\sound\\misc");
			if(dom) cl.copy("sound\\miscdom", tfdir + "\\sound\\misc");
			if(ann) cl.copy("sound\\miscann", tfdir + "\\sound\\misc");
		}
	}
	
	public void restoreMisc() {
		while(checkMisc()) {
			cl.rmdir(tfdir + "\\sound\\misc");
			cl.rename(tfdir + "\\sound\\lwrtmisc", "misc");
		}
	}
	
	public void replaceSkybox(final String filename) {
		while(!cl.exists(tfdir + "\\materials")) 
			cl.mkdir(tfdir + "\\materials");
		
		while(!checkSkybox()) {	
			if(!cl.rename(tfdir + "\\materials\\Skybox", "lwrtSkybox")) cl.mkdir(tfdir + "\\materials\\lwrtSkybox");
			while(!cl.exists(tfdir + "\\materials\\Skybox")) 
				cl.mkdir(tfdir + "\\materials\\Skybox");
		
			FilenameFilter vmtfilter = new FilenameFilter() {
				public boolean accept(File dir, String name) {
					return name.endsWith(".vmt");
				}
			};
		
			String[] vmtfiles = new File("Skybox").list(vmtfilter);
		
			for(int i = 0; i < vmtfiles.length; ++i) {
				cl.copy("Skybox\\" + vmtfiles[i], tfdir + "\\materials\\Skybox\\" + vmtfiles[i]);
			}
		
			FilenameFilter vtffilter = new FilenameFilter() {
				public boolean accept(File dir, String name) {
					return name.startsWith(filename);
				}
			};
		
			String[] vtffiles = new File("Skybox").list(vtffilter);
		
			for(int i = 0; i < vtffiles.length; ++i) {
				for(int j = 0; j < vmtfiles.length; ++j) {
					if(vtffiles[i].endsWith("up.vtf") && vmtfiles[j].endsWith("up.vmt") ||
						vtffiles[i].endsWith("dn.vtf") && vmtfiles[j].endsWith("dn.vmt") ||
						vtffiles[i].endsWith("bk.vtf") && vmtfiles[j].endsWith("bk.vmt") ||
						vtffiles[i].endsWith("ft.vtf") && vmtfiles[j].endsWith("ft.vmt") ||
						vtffiles[i].endsWith("lf.vtf") && vmtfiles[j].endsWith("lf.vmt") ||
						vtffiles[i].endsWith("rt.vtf") && vmtfiles[j].endsWith("rt.vmt")) {
					
						cl.copy("Skybox\\" + vtffiles[i], tfdir + "\\materials\\Skybox\\" + vmtfiles[j].substring(0, vmtfiles[j].indexOf(".vmt")) + ".vtf");
					}
				}
			}
		}
	}
	
	public void restoreSkybox() {
		while(checkSkybox()) {
			cl.rmdir(tfdir + "\\materials\\Skybox");
			cl.rename(tfdir + "\\materials\\lwrtSkybox", "Skybox");
		}
	}
	
	public boolean checkCfg() {
		return cl.exists(tfdir + "\\lwrtcfg");
	}
	
	public boolean checkResource() {
		return cl.exists(tfdir + "\\lwrtresource");
	}
	
	public boolean checkScripts() {
		return cl.exists(tfdir + "\\lwrtscripts");
	}
	
	public boolean checkVo() {
		return cl.exists(tfdir + "\\sound\\lwrtvo");
	}
	
	public boolean checkMisc() {
		return cl.exists(tfdir + "\\sound\\lwrtmisc");
	}
	
	public boolean checkSkybox() {
		return cl.exists(tfdir + "\\materials\\lwrtSkybox");
	}
}
